#include "formpz_fpcy.h"
#include "ui_formpz_fpcy.h"
#include "mysql.h"
#include "QKeyEvent"
FormPZ_FPCY::FormPZ_FPCY(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormPZ_FPCY)
{
    ui->setupUi(this);
    this->show();
    ui->cmbFPLX->clear();
    ui->cmbFPLX->addItem("01-增值税专用发票");
    ui->cmbFPLX->addItem("04-增值税普通发票");
    ui->cmbFPLX->addItem("10-增值税电子普通发票");
    ui->cmbFPLX->setCurrentIndex(0);
    ui->treeWidget->clear();
    ui->treeWidget->setHeaderLabels(QStringList() << "发票类型"<<"发票代码"  << "发票号" << "发票金额" <<"原始信息" );
}

FormPZ_FPCY::~FormPZ_FPCY()
{
    delete ui;
}

void FormPZ_FPCY::on_cmdReturn_clicked()
{
    QString tmp="";
    QTreeWidgetItemIterator it(ui->treeWidget);
    while (*it) {
         //(*it)->text(0);
        tmp+=QString("%1\n").arg((*it)->text(4));
        ++it;
    }
    //QMessageBox::information(NULL, "文件名", tmp);
    emit sendData(tmp);

    this->close();
}


void FormPZ_FPCY::keyPressEvent(QKeyEvent *keyevent){

    if(QApplication::focusWidget() == ui->txtYSXX) {
        ui->txtFPDM->setText("");
        ui->txtFPH->setText("");
        ui->txtFPJE->setText("");
        ui->txtRQ->setText("");

        int uKey = keyevent->key();
        Qt::Key key = static_cast<Qt::Key>(uKey);
        if(key==Qt::Key_Return || key==Qt::Key_Enter ){
            QString JG="";
            //通过判断回车来保存电子发票条形码
            QString str=ui->txtYSXX->text();
            //QMessageBox::information(NULL, "文件名", str);
            str+=",";

            str=str.replace("，",",");
            ui->txtYSXX->setText(str);
            QStringList FPinfo =str.split(",");

            if (FPinfo.count()>8){
                //获取完整信息后开始保存
                //for (int i=0; i<sl.count(); i++)
                //    qDebug() << sl.at(i);
                //先找到发票类型
                for (int i=0; i<ui->cmbFPLX->count(); i++){
                    if (ui->cmbFPLX->itemText(i).left(2)==FPinfo.at(1).right(2)){
                        ui->cmbFPLX->setCurrentIndex(i);
                    }
                }
                ui->txtFPDM->setText(FPinfo.at(2));
                ui->txtFPH->setText(FPinfo.at(3));
                ui->txtFPJE->setText(FPinfo.at(4));

                QString tmp=FPinfo.at(5).left(4);
                tmp+="-";
                tmp+=FPinfo.at(5).mid(4,2);
                tmp+="-";
                tmp+=FPinfo.at(5).mid(6,2);
                ui->txtRQ->setText(tmp);
                QSqlQuery query(mySQL::cn); //查询 表并输出，测试能否正常操作数据库
                QString SQL="";
                int InvoiceCount=0;

                //检查发票是否已经被登记了
                SQL="select reidd from invoice_info ";
                SQL+=QString(" where type_id='%1' and invoice_code='%2' and invoice_no='%3'  ").arg(mySQL::TypeId).arg(ui->txtFPDM->text()).arg(ui->txtFPH->text());

                query.exec(SQL);
                while(query.next()){

                    InvoiceCount++;
                }
                //遍历tree是否重复导入数据
                bool f=false;
                {
                    QTreeWidgetItemIterator it(ui->treeWidget);
                    while (*it) {
                         //(*it)->text(0);
                        if ((*it)->text(1)==ui->txtFPDM->text() && (*it)->text(2)==ui->txtFPH->text()){
                            f=true;
                        }
                        ++it;
                    }
                }
                if (f==true){
                    JG="重复查验！";
                }
                if (InvoiceCount!=0){
                    JG=QString("已使用[%1]").arg(InvoiceCount);
                }
                if (FPinfo.at(0).length()!=2 || FPinfo.at(1).length()!=2 ){
                    JG="再扫一次！";
                    f=true;
                }

                if (f==false && InvoiceCount==0){ //没有导入则加入treeview中
                    //<< "核验结果"<<"发票代码"  << "发票号" << "发票金额" <<"原始信息" );
                    QStringList child;
                    child<< ui->cmbFPLX->currentText();
                    child<<ui->txtFPDM->text();
                    child<<ui->txtFPH->text();
                    child<<ui->txtFPJE->text();
                    child<<ui->txtYSXX->text();
                    QTreeWidgetItem * citem =new QTreeWidgetItem(child);
                    if (InvoiceCount==0){
                        citem->setCheckState(0, Qt::Checked);
                    }else{
                       citem->setCheckState(0, Qt::Unchecked);
                    }
                    ui->treeWidget->addTopLevelItem(citem);
                    JG="通过";
                }
            }
            double je=0;
            {

                QTreeWidgetItemIterator it(ui->treeWidget);
                while (*it) {
                     //(*it)->text(0);
                    je+=(*it)->text(3).toDouble();
                    ++it;
                }
            }
            ui->txtYSXX->selectAll();        // 不起使用,只有在窗体获取到焦点时才有功能
            ui->txtYSXX->setFocus(Qt::OtherFocusReason);
            ui->labJG->setText(QString("%1\n合计：%2").arg(JG).arg(je, 3, 'f', 2));
        }
    }
}

void FormPZ_FPCY::on_cmdScanQR_clicked()
{
    ui->txtYSXX->selectAll();        // 不起使用,只有在窗体获取到焦点时才有功能
    ui->txtYSXX->setFocus(Qt::OtherFocusReason);

}

void FormPZ_FPCY::on_cmdClear_clicked()
{
    ui->treeWidget->clear();
    ui->treeWidget->setHeaderLabels(QStringList() << "核验结果"<<"发票代码"  << "发票号" << "发票金额" <<"原始信息" );
}
